import { baseRequestClient, requestClient } from '../request';
import type { PagedList, PagedRequest } from '../types';

export namespace LogApi {
  /** 登录日志DTO */
  export interface LoginLogDto {
    id: string;
    userName: string;
    ipAddress: string;
    browser: string | null;
    os: string | null;
    status: boolean;
    message: string;
    errorMessage: string | null;
    loginTime: string;
    level: string;
    operationType: string;
  }

  /** 分页查询参数 */
  export interface LoginLogPagedRequest {
    pageIndex: number;
    pageSize: number;
    userName?: string;
    status?: boolean;
    startTime?: string;
    endTime?: string;
    orderBy?: string;
    isDescending?: boolean;
    keyword?: string;
  }

  /** 异常日志DTO */
  export interface ExceptionLogDto {
    id: string;
    exceptionType: string;
    exceptionMessage: string;
    exceptionTime: string;
    requestPath: string;
    ipAddress: string;
    severity: number;
  }

  /** 异常日志详情DTO */
  export interface ExceptionLogDetailDto extends ExceptionLogDto {
    stackTrace: string;
    source: string;
    traceId: string;
    requestMethod: string;
    level: string;
    browser: string;
    os: string;
  }

  /** 异常日志统计DTO */
  export interface ExceptionStatsDto {
    severity: number;
    count: number;
    date: string;
    tenantId?: string;
  }

  /** 异常日志查询参数 */
  export interface ExceptionLogPagedRequest extends PagedRequest {
    exceptionType?: string;
    severity?: number;
    requestPath?: string;
    traceId?: string;
    exceptionMessage?: string;
    ipAddress?: string;
    startTime?: string;
    endTime?: string;
    keyword?: string;
  }

  /** 异常日志统计DTO（前端格式） */
  export interface ExceptionStatsFrontendDto {
    date: string;
    severity0Count: number;
    severity1Count: number;
    severity2Count: number;
    severity3Count: number;
  }
}

/**
 * 分页查询登录日志
 */
export async function getLoginLogsApi(params: LogApi.LoginLogPagedRequest) {
  return requestClient.get<PagedList<LogApi.LoginLogDto>>('/logs/login', { params });
}

/**
 * 获取最近一周登录日志
 */
export async function getRecentLoginLogsApi() {
  return requestClient.get<PagedList<LogApi.LoginLogDto>>('/logs/login/recent');
}

/**
 * 按用户名搜索登录日志
 */
export async function getUserLoginLogsApi(userName: string) {
  return requestClient.get<PagedList<LogApi.LoginLogDto>>(`/logs/login/by-user/${userName}`);
}

/**
 * 清理指定时间之前的日志
 */
export async function cleanLoginLogsByTimeApi(beforeTime: string) {
  return requestClient.delete<number>('/logs/login/clean', { params: { beforeTime } });
}

/**
 * 清理指定天数之前的日志
 */
export async function cleanLoginLogsByDaysApi(days: number) {
  return requestClient.delete<number>(`/logs/login/clean/${days}`);
}

/**
 * 分页查询异常日志
 */
export async function getExceptionLogsApi(params: LogApi.ExceptionLogPagedRequest) {
  return requestClient.get<PagedList<LogApi.ExceptionLogDto>>('/logs/exception', { params });
}

/**
 * 获取最近异常日志
 */
export async function getRecentExceptionLogsApi(hours: number = 24) {
  return requestClient.get<PagedList<LogApi.ExceptionLogDto>>('/logs/exception/recent', { params: { hours } });
}

/**
 * 获取异常日志详情
 */
export async function getExceptionLogDetailApi(id: string) {
  return requestClient.get<LogApi.ExceptionLogDetailDto>(`/logs/exception/${id}`);
}

/**
 * 获取异常统计信息（前端格式）
 */
export async function getExceptionStatsApi(startTime?: string, endTime?: string) {
  return requestClient.get<LogApi.ExceptionStatsFrontendDto[]>('/logs/exception/frontend-stats', {
    params: { startTime, endTime }
  });
}

/**
 * 清理指定天数之前的异常日志
 */
export async function cleanExceptionLogsByDaysApi(days: number) {
  return requestClient.delete<number>(`/logs/exception/clean/${days}`);
}

/**
 * 批量删除异常日志
 */
export async function batchDeleteExceptionLogsApi(ids: string[]) {
  return requestClient.delete<number>('/logs/exception/batch', { data: ids });
}

/**
 * 导出异常日志
 */
export async function exportExceptionLogsApi(params: LogApi.ExceptionLogPagedRequest) {
  return requestClient.get('/logs/exception/export', {
    params,
    responseType: 'blob'
  });
}
